﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Way.Bus.WMS.Controllers;
using Way.Bus.WMS.SF.Model;
using Way.Bus.WMS.Model;
using Way.EAP.DataAccess.Entity;
using Way.Contract.MVC;
using System.Data;
using System.Configuration;
using InterfaceService.DataAccess;
using Way.Contract.Data;
using Way.Bus.Controllers;
using Way.Bus.Model;

namespace Way.Bus.WMS.SF.Controllers
{
    public class CustomerController : BusinessController
    {
        public CustomerController() : base(new BusinessCustomer()) { }
        //显示在菜单
        protected override string OnControllerName()
        {
            return "客户管理管理";
        }
        //是否启用回收站
        public override bool IsClearAway
        {
            get
            {
                return false;
            }
        }
        //菜单中url路径
        protected override string OnGetPath()
        {
            return "Views/SF/Customer.htm";
        }
        //开发人
        protected override string OnGetAuthor()
        {
            return "路聪";
        }
        //说明
        protected override string OnGetControllerDescription()
        {
            return "客户管理";
        }
        #region 初始化数据方法
        protected override void OnInitViewChildItem(Contract.Data.ModelData data, Contract.Data.ItemData item)
        {
            base.OnInitViewChildItem(data, item);
            if (data.name.EndsWith("BusinessCustomer"))
            {
                switch (item.field.ToLower())
                {
                    case "code":
                    case "customername":
                        item.title = "客户名称";
                        break;
                    case "fphone":
                    case "createdby":
                    case "submitedby":
                        item.visible = true;
                        break;
                    default:
                        item.visible = false;
                        break;
                }
            }
        }
        #endregion


        protected override ActionCommand[] OnInitCommand(ActionCommand[] commands)
        {
            //获取所有按钮集合
            foreach (ActionCommand ac in commands)
            {
                if (ac.command == "SubmitData" || ac.command == "OnSubmitData" || ac.command == "ReviewedData" || ac.command == "ImportExcel")
                {
                    ac.visible = false;
                }
            }
            return commands;
        }

        protected override void OnAdded(Way.EAP.DataAccess.Entity.EntityBase item)
        {
            base.OnAdded(item);
            BusinessCustomer reg = item as BusinessCustomer;
            InsertOrUpdateQyk(reg);
        }

        protected override void OnEditing(Contract.MVC.ControllerBase.SaveEvent item)
        {
            base.OnEditing(item);
            BusinessCustomer reg = item.Item as BusinessCustomer;
            InsertOrUpdateQyk(reg);
        }

        protected override void OnRemoveing(Contract.MVC.ControllerBase.SaveEvent item)
        {
            BusinessCustomer reg = item.Item as BusinessCustomer;
            DeleteQyk(reg);
            base.OnRemoveing(item);
        }

        private void InsertOrUpdateQyk(BusinessCustomer reg)
        {
            string strQyh = reg.ID.ToString().PadLeft(4, '0');
            string strSql = "SELECT * from Dwh where Dwh='" + strQyh + "'";
            string strCon = ConfigurationManager.AppSettings["FOXJC"].ToString() + "DWH.DBF" + ConfigurationManager.AppSettings["ENDCONN"].ToString();
            DataTable dt = BusFoxProHelper.ExecuteDataTable(strCon, CommandType.Text, strSql);
            if (dt.Rows.Count == 0)
                strSql = "insert into Dwh(Dwh,Dwm,Dwmc,Slfl,Vrl) values('" + strQyh + "','" + reg.Code + "','" + reg.CUSTOMERNAME + "','',0.000)";
            else
                strSql = "update Dwh set Dwm='" + reg.Code + "',Dwmc='" + reg.CUSTOMERNAME + "' where Dwh='" + strQyh + "'";
            BusFoxProHelper.ExecuteNonQuery(strCon, CommandType.Text, strSql);
        }

        private void DeleteQyk(BusinessCustomer reg)
        {
            EntityList<BusinessCustomer> list = new EntityList<BusinessCustomer>(this.model.GetDataAction());
            list.GetData("CUSTOMERNAME='" + reg.CUSTOMERNAME + "'");
            string strQyh = list[0].ID.ToString().PadLeft(4, '0');
            string strSql = "update Dwh set Dwh='',Dwm='',Dwmc='' where Dwh='" + strQyh + "'";
            string strCon = ConfigurationManager.AppSettings["FOXJC"].ToString() + "DWH.DBF" + ConfigurationManager.AppSettings["ENDCONN"].ToString();
            BusFoxProHelper.ExecuteNonQuery(strCon, CommandType.Text, strSql);
        }

    }
}
